package com.mapaower.admin.controller;

import com.mapaower.admin.entity.Permission;
import com.mapaower.admin.service.PermissionService;
import com.mapaower.admin.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/permissions")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取所有权限列表
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return ResponseEntity.ok(ResponseResult.success(permissions));
    }

    /**
     * 根据ID获取权限信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> getPermissionById(@PathVariable Long id) {
        Permission permission = permissionService.getPermissionById(id);
        if (permission != null) {
            return ResponseEntity.ok(ResponseResult.success(permission));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据权限标识获取权限信息
     */
    @GetMapping("/key/{permissionKey}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> getPermissionByKey(@PathVariable String permissionKey) {
        Permission permission = permissionService.getPermissionByKey(permissionKey);
        if (permission != null) {
            return ResponseEntity.ok(ResponseResult.success(permission));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新权限
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> createPermission(@RequestBody Permission permission) {
        try {
            Permission createdPermission = permissionService.createPermission(permission);
            return ResponseEntity.ok(ResponseResult.success(createdPermission));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }

    /**
     * 更新权限信息
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> updatePermission(@PathVariable Long id, @RequestBody Permission permission) {
        try {
            Permission updatedPermission = permissionService.updatePermission(id, permission);
            return ResponseEntity.ok(ResponseResult.success(updatedPermission));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> deletePermission(@PathVariable Long id) {
        boolean result = permissionService.deletePermission(id);
        if (result) {
            return ResponseEntity.ok(ResponseResult.success("权限已删除"));
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}